<template>
<!-- 	<div class="code-preview">
		<el-dialog v-model="show" width="760px" title="发起退款">
			<el-form ref="formRef" :model="formData" label-width="120px">
				<el-form-item label="支付金额">¥ {{ props.payPrice }} </el-form-item>
				<el-form-item label="退款金额" prop="num">
					<el-input v-model="formData.num" />
				</el-form-item>
				<el-form-item label="备注" prop="remark">
					<el-input v-model="formData.remark" type="textarea" :rows="4" />
				</el-form-item>
				<el-button>发起退款</el-button>
			</el-form>
		</el-dialog>
	</div> -->
	<div class="edit-popup">
		<popup ref="popupRef" title="退款" :async="true" width="550px" @confirm="handleSubmit"
			@close="handleClose">
<el-form ref="formRef" :model="formData" label-width="120px">
				<el-form-item label="法人姓名">{{ formData.oper_name }} </el-form-item>
				<el-form-item label="支付金额">¥ {{ formData.order_amount }} </el-form-item>
				<el-form-item label="退款原因">{{ formData.refund_reason }} </el-form-item>
				<el-form-item label="退款金额" prop="num">
					<el-input v-model="formData.refundPrice" />
				</el-form-item>
				<el-form-item label="退款备注" prop="remark">
					<el-input v-model="formData.remark" type="textarea" :rows="4" />
				</el-form-item>
				<el-button v-if="formData.type" type="danger" style="float: right;" @click="canalButton">取消/拒绝退款</el-button>
				<div class="form-tips" style="color: red;float: right;margin-right: 20px;">点击下方确定按钮之后不可取消退款</div>
			</el-form>
		</popup>
	</div>
</template>

<script lang="ts" setup>
	import { refundSet,canalRefund } from '@/api/finance'
	import type { FormInstance, FormRules } from 'element-plus'
		import Popup from '@/components/popup/index.vue'
		import type { PropType } from 'vue'
		
	defineProps({
		dictData: {
			type: Object as PropType<Record<string, any[]>>,
			default: () => ({})
		}
	})
	const emit = defineEmits(['success', 'close'])
	const formRef = shallowRef<FormInstance>()
	const popupRef = shallowRef<InstanceType<typeof Popup>>()
	const mode = ref('add')

	const formData = reactive({
		order_amount: '',
		id: '',
		remark:'',
		refundPrice:'',
		refund_reason:'',
		refund_id:'',
		type:'',
		refund_type:'',
		oper_name:''
	})
	const refundPrice = ref('')
	// 获取详情
	const setFormData = async (data : Record<any, any>) => {
		for (const key in formData) {
			if (data[key] != null && data[key] != undefined) {
				//@ts-ignore
				formData[key] = data[key]
			}
		}
		formData.refundPrice=formData.order_amount
	
	
	}
	// 提交按钮
	const handleSubmit = async () => {
		await formRef.value?.validate()
		const data = { ...formData, }
		console.log(data)
		await refundSet({
			recharge_id: data.id,
			refund_price: data.refundPrice,
			refund_type:data.refund_type
		})
		popupRef.value?.close()
		emit('success')
	}
	
	const canalButton = async () => {
		const data = { ...formData, }
		console.log(data)
		await canalRefund({
			id: data.refund_id,
		})
		popupRef.value?.close()
		emit('success')
	}
	
	//打开弹窗
	const open = (type = 'add') => {
		mode.value = type
		popupRef.value?.open()
	}
	
	// 关闭回调
	const handleClose = () => {
		emit('close')
	}
	
	
	
	defineExpose({
		open,
		setFormData,
	})
</script>